﻿/******************************************************************************************
*                                                                                         *    
*                              Developed by Bruno Pires                                   *
*                                                                                         *
*                             email: bruno@blastersystems.com                             *
*                               web: www.blastersystems.com                               *
*                              blog: www.blastersystems.com/blog                          *
*                           twitter: @brunoacpires                                        *
*                                                                                         *
*             Software available under GNU LGPL v3 License without any guarantee          *
*                                                                                         *    
* *****************************************************************************************/ 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using OpenNI;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.Windows;
using System.IO;

namespace Pap.Service.ImageVGA
{
    public class PapVGA
    {
        public WriteableBitmap StartDataProcessing(Context ctx, ImageGenerator generator)
        {
            if (generator != null)
            {
                var mapMode = generator.MapOutputMode;
                return new WriteableBitmap(mapMode.XRes, mapMode.YRes, 96, 96, PixelFormats.Rgb24, null);
            }
            else
            {
                throw new Exception("There is no node defined for RGB, verify OpenNI config file.");
            }
        }

        public WriteableBitmap UpdateRgb(ImageGenerator generator, WriteableBitmap rgbBitmap)
        {
            rgbBitmap.Lock();
            var rect = new Int32Rect(0, 0, (int)rgbBitmap.Width, (int)rgbBitmap.Height);
            rgbBitmap.WritePixels(rect, generator.ImageMapPtr, (int)generator.DataSize, rgbBitmap.BackBufferStride);
            rgbBitmap.Unlock();

            return rgbBitmap;
        }
    }
}
